package cn.maidouya.org.controller;

import cn.maidouya.basic.util.AjaxResult;
import cn.maidouya.basic.util.PageLIst;
import cn.maidouya.org.domain.Employee;
import cn.maidouya.org.query.EmployeeQuery;
import cn.maidouya.org.service.EmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employee")
@Api(tags = "员工接口",description = "员工接口详细描述")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    /**
     *  根据id查询一个员工
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查找员工",notes = "根据ID查找员工")
    public Employee loadById(@PathVariable("id") Long id){
        Employee employeeOne = employeeService.loadById(id);
        return employeeOne;
    }

    /**
     *  查询所有的员工
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查找全部员工",notes = "查找所有员工信息")
    public List<Employee> loadAll(){
        return employeeService.loadAll();
    }
    /**
     *   查询分页和总条数
     * @return
     */
    @PostMapping
    @ApiOperation(value = "查找员工分页数据",notes = "分页查找所有员工信息")
    public PageLIst<Employee> queryEmployeePage(@RequestBody EmployeeQuery employeeQuery){
        PageLIst<Employee> pageLIst = employeeService.queryData(employeeQuery);
        return pageLIst;
    }

    /**
     *  添加和修改员工
     * @param employee
     * @return
     */
    @PutMapping
    @ApiOperation(value = "添加和修改员工",notes = "添加和修改员工")
    public AjaxResult saveAndUpdata(@RequestBody Employee employee){
        try {
            if (employee.getId() == null){
                employeeService.save(employee);
            } else {
                employeeService.update(employee);
            }
            return AjaxResult.getResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getResult().setSuccess(false).setMessage("操作失败");
        }
    }
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除员工",notes = "根据id删除员工")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            employeeService.delete(id);
            return AjaxResult.getResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getResult().setSuccess(false).setMessage("删除失败");
        }
    }

    /**
     *   批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除员工",notes = "根据id批量删除员工")
    public AjaxResult batDel(@RequestBody Long[] ids){
        try {
            employeeService.batDel(ids);
            return AjaxResult.getResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getResult().setSuccess(false).setMessage("批量删除失败");
        }

    }
}